package br.com.compraentrega.cbreader.visao;

import java.text.DecimalFormat;
import javax.swing.JTextField;

/**
 *
 * @author Henrique Pontes
 * @version 0.1 08/17/2010
 */
public class GuiCadastrar extends javax.swing.JPanel {

    private DecimalFormat mascaraPreco = new DecimalFormat("###,##0.00");

    /** Cria o form GuiCadastrar, iniciando todos os componentes. */
    public GuiCadastrar() {
        initComponents();
        campoPreco.setHorizontalAlignment(JTextField.RIGHT);
    }

    private void limparCampos() {

        campoCodigo.setText(null);
        campoPreco.setText(null);

    }

    private void comportamentoSalvar() {

        campoUltimoCodigo.setText(campoCodigo.getText());
        campoUltimoPreco.setText(campoPreco.getText());
        limparCampos();

    }
    /**
     * Método gerado pelo netbeans para iniciar todos os componentes.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rotuloCodigo = new javax.swing.JLabel();
        rotuloPreco = new javax.swing.JLabel();
        campoCodigo = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        rotuloItemAdicionado = new javax.swing.JLabel();
        campoUltimoCodigo = new javax.swing.JTextField();
        rotuloUltimoCodigo = new javax.swing.JLabel();
        rotuloUltimoPreco = new javax.swing.JLabel();
        campoUltimoPreco = new javax.swing.JTextField();
        botaoSalvar = new javax.swing.JButton();
        botaoLimpar = new javax.swing.JButton();
        botaoEditar = new javax.swing.JButton();
        campoPreco = new javax.swing.JFormattedTextField(mascaraPreco);
        boxSupermercados = new javax.swing.JComboBox();
        rotuloSupermercado = new javax.swing.JLabel();

        rotuloCodigo.setText("Código:");

        rotuloPreco.setText("Preço:");

        campoCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoCodigoKeyPressed(evt);
            }
        });

        rotuloItemAdicionado.setText("Último ítem adicionado:");

        campoUltimoCodigo.setEnabled(false);

        rotuloUltimoCodigo.setText("Código:");

        rotuloUltimoPreco.setText("Preco:");

        campoUltimoPreco.setEnabled(false);

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });
        botaoSalvar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                botaoSalvarKeyPressed(evt);
            }
        });

        botaoLimpar.setText("Limpar");
        botaoLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoLimparActionPerformed(evt);
            }
        });

        botaoEditar.setText("Editar");
        botaoEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoEditarActionPerformed(evt);
            }
        });

        campoPreco.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoPrecoKeyPressed(evt);
            }
        });

        boxSupermercados.setModel(new javax.swing.DefaultComboBoxModel(br.com.compraentrega.cbreader.persistencia.SupermercadosTemp.supermercadosLista));

        rotuloSupermercado.setText("Supermercado:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rotuloItemAdicionado)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rotuloUltimoCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoUltimoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rotuloUltimoPreco)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoUltimoPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botaoEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(126, 126, 126))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botaoLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(63, 63, 63))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(rotuloSupermercado)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(boxSupermercados, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(rotuloCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rotuloPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoPreco, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)))
                .addGap(16, 16, 16))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloSupermercado)
                    .addComponent(boxSupermercados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(rotuloPreco)
                    .addComponent(rotuloCodigo)
                    .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(botaoLimpar))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rotuloItemAdicionado)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloUltimoCodigo)
                    .addComponent(campoUltimoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rotuloUltimoPreco)
                    .addComponent(campoUltimoPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(botaoEditar)
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Ações para o evento disparado pelo botão Salvar.
     * @param evt
     */
    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed

        comportamentoSalvar();

    }//GEN-LAST:event_botaoSalvarActionPerformed

    /**
     * Ações para o evento disparado pelo botão Limpar.
     * @param evt
     */
    private void botaoLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoLimparActionPerformed

        limparCampos();

    }//GEN-LAST:event_botaoLimparActionPerformed

    /**
     * Ações para o evento disparado pelo botão Editar.
     * @param evt
     */
    private void botaoEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoEditarActionPerformed

        limparCampos();
        campoCodigo.setText(campoUltimoCodigo.getText());
        campoPreco.setText(campoUltimoPreco.getText());

    }//GEN-LAST:event_botaoEditarActionPerformed

    private void campoCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoCodigoKeyPressed

        if (evt.getKeyCode() == 10) {
            campoPreco.requestFocus();
        }
        
    }//GEN-LAST:event_campoCodigoKeyPressed

    private void botaoSalvarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_botaoSalvarKeyPressed

        if (evt.getKeyCode() == 10) {
            comportamentoSalvar();
        }
        
    }//GEN-LAST:event_botaoSalvarKeyPressed

    private void campoPrecoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoPrecoKeyPressed

        if (evt.getKeyCode() == 10) {
            botaoSalvar.requestFocus();
        }

    }//GEN-LAST:event_campoPrecoKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoEditar;
    private javax.swing.JButton botaoLimpar;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JComboBox boxSupermercados;
    private javax.swing.JTextField campoCodigo;
    private javax.swing.JFormattedTextField campoPreco;
    private javax.swing.JTextField campoUltimoCodigo;
    private javax.swing.JTextField campoUltimoPreco;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel rotuloCodigo;
    private javax.swing.JLabel rotuloItemAdicionado;
    private javax.swing.JLabel rotuloPreco;
    private javax.swing.JLabel rotuloSupermercado;
    private javax.swing.JLabel rotuloUltimoCodigo;
    private javax.swing.JLabel rotuloUltimoPreco;
    // End of variables declaration//GEN-END:variables

}
